package com.example.mymarket.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.mymarket.Data_model.CurrentCategory;
import com.example.mymarket.Data_model.currentSubCategory;
import com.example.mymarket.R;

import java.util.List;

public class currentSubCategoryAdapter extends BaseAdapter {

    private Context context;
    private List<CurrentCategory> currentSubCategoryList;

    public currentSubCategoryAdapter(Context context, List<CurrentCategory> currentSubCategoryList) {
        this.context = context;
        this.currentSubCategoryList = currentSubCategoryList;
    }

    @Override
    public int getCount() {
        return currentSubCategoryList == null ? 0 : currentSubCategoryList.size();
    }

    @Override
    public Object getItem(int i) {
        return currentSubCategoryList == null ? null : currentSubCategoryList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Log.e("TAG", "进入getView" );
        ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.fragment_catalog_content_item, viewGroup, false);
            holder = new ViewHolder();
            holder.ivIcon = view.findViewById(R.id.iv_category_icon);
            holder.tvName = view.findViewById(R.id.tv_category_name);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        CurrentCategory subCategory = currentSubCategoryList.get(i);
        Glide.with(context)
                .load(subCategory.getPicUrl())
                .into(holder.ivIcon);
        holder.tvName.setText(subCategory.getName());
        return view;
    }

    static class ViewHolder {
        ImageView ivIcon;
        TextView tvName;
    }
}

